package cc.mrbird.febs.activity.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.mrbird.febs.activity.entity.Activity;
import cc.mrbird.febs.activity.mapper.ActivityMapper;
import cc.mrbird.febs.activity.service.IActivityService;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;

/**
 * 活动信息表 Service实现
 *
 * @author Lord
 * @date 2020-06-06 16:11:26
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Override
    public IPage<Activity> findActivitys(QueryRequest request, Activity activity) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(activity);
        Page<Activity> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Activity> findActivitys(Activity activity) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(activity);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createActivity(Activity activity) {
        this.save(activity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateActivity(Activity activity) {
        this.saveOrUpdate(activity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivity(Activity activity) {
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.setEntity(activity);
        this.remove(wrapper);
    }

    @Override
    public void deleteActivitys(String[] activityIds) {
        List<String> list = Arrays.asList(activityIds);
        this.removeByIds(list);
    }
}
